<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2018 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Released under the MIT License.
// +----------------------------------------------------------------------
// | Author: 老猫 <thinkcmf@126.com>
// +----------------------------------------------------------------------

namespace app\app\controller;

use cmf\controller\AdminBaseController;
use think\Db;
use app\admin\model\AdminMenuModel;
use think\Validate;
class GoodsController extends AdminBaseController
{
    public function index()
    {
        $where = array();
        $params = $this->param();
        if(isset($params['name'])){
            $where[] = ['t1.name','like','%'.$params['name'].'%'];
        }
        if(isset($params['status']) && $params['status']){
            $where[] = ['t1.status','eq',$params['status']];
            $this->assign('status',$params['status']);
        }
        $goods = db::name('goods')->alias('t1')
            ->join('good_cate t2','t2.id=t1.cateId')
            ->field('t1.*,t2.name as title')
            ->where($where)
            ->order('t1.id desc')
            ->paginate(10);
        $this->assign('goods',$goods);
        $goods->appends($params);  //分页条件
        $this->assign('page', $goods->render());
        return $this->fetch();
    }

    public function add()
    {
        if($_POST){
            $validate = new Validate([
                'name'  => 'require',
                'price' => 'require',
                'para' => 'require',
                'para1' => 'require',
                'content' => 'require',
                'img' => 'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            $data['content'] = $this->doimg($_POST['content']);
            $data['img'] = '/upload/'.$_POST['img'];
            $data['name'] = $_POST['name'];
            $data['price'] = $_POST['price'];
            $data['para'] = $_POST['para'];
            $data['para1'] = $_POST['para1'];
            $data['cateId'] = $_POST['cateId'];
            if($_POST['sales']){
                $data['sales'] = $_POST['sales'];
            }
            if($_POST['old_price']){
                $data['old_price'] = $_POST['old_price'];
            }
            $data['addtime'] = date('Y-m-d H:i:s',time());
            $res = db::name('goods')->insert($data);
            if($res){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
        //dump(config()['app']);
        $cate = db::name('good_cate')->select();
        $this->assign('cate',$cate);
        return $this->fetch();
    }
    public function edit()
    {
        $id = $this->param('id');
        $find = db::name('goods')->find($id);
        if($_POST){
            $validate = new Validate([
                'name'  => 'require',
                'price' => 'require',
                'para' => 'require',
                'para1' => 'require',
                'content' => 'require',
            ]);
            if(!$validate->check($_POST)){
                $this->error($validate->getError());
            }
            //首先验证 详情是否修改
            if($find['content'] !== $_POST['content']){
                //修改过
                $data['content'] = $this->checkimg($_POST['content']);
            }
            if($_POST['img']){
                $data['img'] = '/upload/'.$_POST['img'];
            }
            $data['name'] = $_POST['name'];
            $data['price'] = $_POST['price'];
            $data['para'] = $_POST['para'];
            $data['para1'] = $_POST['para1'];
            $data['cateId'] = $_POST['cateId'];
            $data['id'] =  $_POST['id'];
            if($_POST['sales']){
                $data['sales'] = $_POST['sales'];
            }
            if($_POST['old_price']){
                $data['old_price'] = $_POST['old_price'];
            }
            $res = db::name('goods')->update($data);
            if($res){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');
            }

        }else{
            $cate = db::name('good_cate')->select();
            $this->assign('cate',$cate);
            $this->assign('info',$find);
            return $this->fetch();
        }
    }
    public function delete()
    {
        $id = $this->param('id');
        $res = db::name('goods')->delete($id);
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    public function update()
    {
        $data['id'] = (int)$this->param('id');
        $data['status'] = 2;
        $res = db::name('goods')->update($data);
        if($res){
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }
    //处理富文本中的img标签
    private function doimg($str)
    {
        preg_match_all("/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i",$str,$img_array);
        $newstr = $str;
        if($img_array[0]){
            foreach($img_array[2] as $k=>$v){
                $newstr = str_replace($v,config()['app']['url'].$v,$newstr);
            }
        }
        return $newstr;
    }
    //checkimg
    private function checkimg($str)
    {
        preg_match_all("/<\s*img\s+[^>]*?src\s*=\s*(\'|\")(.*?)\\1[^>]*?\/?\s*>/i",$str,$img_array);
        $newstr = $str;
        if($img_array[0]){
            foreach($img_array[2] as $k=>$v){
                //判断图片是否有http
                if(!strstr($v,'http')){
                    $newstr = str_replace($v,config()['app']['url'].$v,$newstr);
                }
            }
        }
        return $newstr;
    }
    //店铺商品
    public function lst()
    {
        $sessionAdminId = session('ADMIN_ID');
        //首先判断当前登陆用户  是店铺还是  超级管理员
        $where = array();
        $params = $this->param();
        if(isset($params['name'])){
            $where[] = ['t1.name','like','%'.$params['name'].'%'];
        }
        if(isset($params['status']) && $params['status']){
            $where[] = ['t1.status','eq',$params['status']];
            $this->assign('status',$params['status']);
        }
        if($sessionAdminId !== 1){
            //说明是店铺登陆
            $find = db::name('app_shop')->where('admin_id',$sessionAdminId)->find();
            $where[] = ['t3.shop_id','eq',$find['id']];
        }
        $goods = db::name('goods')->alias('t1')
            ->join('good_cate t2','t2.id=t1.cateId')
            ->join('app_shopgoods t3','t3.goods_id = t1.id')
            ->join('app_shop t4','t4.id = t3.shop_id')
            ->field('t1.*,t2.name as title,t4.name as shopname,t3.num,t3.is_public as public,t3.id as sid,t3.price as newprice')
            ->where($where)
            ->order('t4.id desc')
            ->paginate(10);
        $this->assign('goods',$goods);
        $goods->appends($params);  //分页条件
        $this->assign('page', $goods->render());
        return $this->fetch();
    }
    public function ledit()
    {
        $id = $this->param();
        $find = db::name('goods')->find($id);
        $cate = db::name('good_cate')->where('id',$find['cateId'])->find();
        $find['catename'] = $cate['name'];
        $this->assign('info',$find);
        return $this->fetch();
    }
    //店铺修改 
    public function kcup()
    {
        $id = $this->param();
        if($_POST){
            $res = db::name('app_shopgoods')->update($_POST);
            if($res){
                $this->success('成功');
            }else{
                $this->error('失败');
            }
        }
        $find = db::name('app_shopgoods')->find($id);
        $this->assign('info',$find);
        return $this->fetch();
    }
}
